/**
 * 
 */
package vn.edu.hungvuongaptech.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.dao.TimeSheetDAO;
import vn.edu.hungvuongaptech.model.TimeSheetModel;

/**
 * @author ThanhHQ
 *
 */
public class FilterTimeSheetController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		long userId = Long.parseLong(req.getSession().getAttribute(Constant.USERID_ATT).toString());
		int roleId = Integer.parseInt(req.getSession().getAttribute(Constant.ROLE_ATT).toString());
		int cboProject = req.getParameter("cboProject").toString() == "" ? 0 : Integer.parseInt(req.getParameter("cboProject").toString());
		int cboStatus = req.getParameter("cboStatus").toString() == "" ? 0 : Integer.parseInt(req.getParameter("cboStatus").toString());
		String txtFromDate = req.getParameter("txtFromDate") == "" ? null : req.getParameter("txtFromDate");
		String txtToDate = req.getParameter("txtToDate") == "" ? null : req.getParameter("txtToDate");
		ArrayList<TimeSheetModel> timeSheetModels = null;
		if (roleId != 3) // NOT Administrator role.
			timeSheetModels = TimeSheetDAO
				.getTimeSheetByCondition(userId, cboProject, cboStatus,
						txtFromDate, txtToDate, 10, 1, 20);
		else {
			long cboUser = req.getParameter("cboUser").toString() == "" ? 0 : Long.parseLong(req.getParameter("cboUser").toString()); 
			timeSheetModels = TimeSheetDAO
			.getTimeSheetByCondition(cboUser, cboProject, cboStatus,
					txtFromDate, txtToDate, 10, 1, 20);
		}
		req.setAttribute("timeSheetModels", timeSheetModels);
		RequestDispatcher dispatcher = req.getRequestDispatcher("SearchTimeSheet.jsp");
		dispatcher.forward(req, resp);
	}
}
